﻿using System;
using SharpDX.Direct3D11;
using System.Windows.Forms;
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.DXGI;
using SharpDX.Windows;
using Device = SharpDX.Direct3D11.Device;
using Resource = SharpDX.Direct3D11.Resource;
using System.Runtime.InteropServices;
using SharpDX.DirectInput;
using SharpDX.Multimedia;
using SharpDX.RawInput;
using System.Collections;
using ModelViewer;


namespace Engine
{
    public static class InputManager
    {
        public static MouseState mouseState;
        public static KeyboardState keyboardState;

        public static Vector2 MousePosition = new Vector2();
        public static Vector2 PrePosition = new Vector2();
        public static Vector2 MouseDelta = new Vector2();

        public static bool LeftMouse = false;
        public static bool RightMouse = false;
        public static bool MidleMouse = false;
        public static int MouseWheel;

        public static bool PreLeftMouse = false;
        public static bool PreRightMouse = false;
        public static bool PreMidleMouse = false;
        public static int PreMouseWheel;

        public static bool CursorToCenter = false;

        public static void Init()
        {
            SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, SharpDX.RawInput.DeviceFlags.None);
            SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericMouse, SharpDX.RawInput.DeviceFlags.None);
            SharpDX.RawInput.Device.KeyboardInput += KeyboardState.DeviceOnKeyboardInput;
            SharpDX.RawInput.Device.MouseInput += Device_MouseInput;
        }



        public static void FreeResource()
        {
        }

        public static void Update(float TimeMili)
        {
            System.Drawing.Point CenterScreen = new System.Drawing.Point(Global.Settings.Width / 2, Global.Settings.Height / 2);
            System.Drawing.Point CenterForm = Program.form.PointToScreen(CenterScreen);
            
            PrePosition = MousePosition;
            System.Drawing.Point lpPoint = Program.form.PointToClient(Cursor.Position);
            MousePosition = new Vector2(lpPoint.X, lpPoint.Y);

            MouseDelta = new Vector2(MousePosition.X - CenterScreen.X, MousePosition.Y - CenterScreen.Y);

            if (CursorToCenter)
            {
                Cursor.Position = CenterForm;// new System.Drawing.Point(100, 100);
                //PrePosition = new Vector2(CenterForm.X, CenterForm.Y);
            }

            keyboardState = KeyboardState.GetCurrentState();
            /*
            if (keyboardState.PressedKeys.Count > 0)
            {
                foreach (Keys pressedKey in keyboardState.PressedKeys)
                {
                    Console.Write(pressedKey + " ");
                }
                Console.WriteLine();
                return;
            }*/

            PreLeftMouse = LeftMouse;
            PreMidleMouse = MidleMouse;
            PreRightMouse = RightMouse;
            PreMouseWheel = MouseWheel;

            LeftMouse = LB;
            RightMouse = RB;
            MidleMouse = MB;
            MouseWheel += WD;
        }

        public static bool KeyPressed(Keys K)
        {
            return keyboardState.PressedKeys.Contains(K);
        }

        static bool LB, MB, RB;
        static int WD;

        private static void Device_MouseInput(object sender, MouseInputEventArgs e)
        {
            int s = (int)e.ButtonFlags;

            if ((s & (int)MouseButtonFlags.LeftButtonDown) > 0) LB = true;
            else if ((s & (int)MouseButtonFlags.LeftButtonUp) > 0) LB = false;

            if ((s & (int)MouseButtonFlags.RightButtonDown) > 0) RB = true;
            else if (((int)s & (int)MouseButtonFlags.RightButtonUp) > 0) RB = false;

            if ((s & (int)MouseButtonFlags.MiddleButtonDown) > 0) MB = true;
            else if ((s & (int)MouseButtonFlags.MiddleButtonUp) > 0) MB = false;

            if ((s & (int)MouseButtonFlags.MouseWheel) > 0) WD += e.WheelDelta;
         }

    }
}
